#include "GL/glut.h"
#include <ctime>
#include "visRayCasting.h"
#include "ucdinputfile.h"

using namespace visualization;

Model md;
visRayCasting img;

#define imageSize 350
unsigned char _image[imageSize][imageSize][3];

void makeImage(void)
{
	int i, j;//, c;
	for(i = 0; i < imageSize; i++)
	{
		for(j = 0; j < imageSize; j++)
		{
			for(int k = 0; k < 3; k++)
				_image[i][j][k] = (img.getImage())[i][j][k];
		}
	}
}
void init ( void )
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glShadeModel(GL_FLAT);
	makeImage();
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}

void display ( void )
{
	glClear(GL_COLOR_BUFFER_BIT);
	glRasterPos2f(-0.97, -0.97);
	glDrawPixels(imageSize, imageSize, GL_RGB, GL_UNSIGNED_BYTE, _image);
	glFlush();
}

void reshape (int w, int h)
{
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ( );
	if (w <= h)
		glOrtho (-1.5, 1.5, -1.5 * ( GLfloat ) h / ( GLfloat ) w,
		1.5 * ( GLfloat ) h / ( GLfloat ) w, -10.0, 10.0 );
	else
		glOrtho (-1.5 * ( GLfloat ) w / ( GLfloat ) h,
		1.5 * ( GLfloat ) w / ( GLfloat ) h, -1.5, 1.5, -10.0, 10.0);
	glMatrixMode ( GL_MODELVIEW );
	glLoadIdentity ( ) ;
}

void keyboard ( unsigned char key, int x, int y)
{
	switch (key) {
		case 27:
		exit ( 0 );
		break;
	}
}


int main(int argc, char** argv)
{
	if(argc < 2)
	{
		cout << "Have you forgeten the file you are supposed to input?" << endl;
		return 0;
	}
	UCDInputFile file(argv[1]);

	cout << "READING DATA..." << endl;
	file.FGetModel(md);
	cout << "BUILDING KDTREE..." << endl;
	md.buildKDTree();

	img.setModel(&md);
//	img.generateTransferFunction();
	img.setImageSize(imageSize);
	img.setSamplingRate();
	double direction[] = {100, 0, 129};
	img.setViewingPosition(direction, 3);
	cout << "RESAMPLING..." << endl;

	time_t begin_sampling, end_sampling;

	begin_sampling = time(NULL);
	img.resample();
	img.combineToFormImage(1);
	end_sampling = time(NULL);

	cout << "Time used for sampling : " << end_sampling - begin_sampling << endl;
	
//	image**result=img.getImage();
//	int size = img.getImageSize();
	
/*	cout << "The image: " << endl;
	for(int i = 0; i < imageSize; i++)
		for(int j = 0; j < imageSize; j++)
		{
			if(i % 100 == 0 && j % 100 == 0)
				cout <<	(int)_image[i][j][0] << "	" << (int)_image[i][j][1] << "	" << (int)_image[i][j][2] << endl; 
		}
*/
//	double point[3] = {39.4338, 39.4338, -3.86751};
/*	double point[3] = {0, 0, 0};
    double direction[3] = {1/sqrt(3), 1/sqrt(3), 1/sqrt(3)};
	double flag = -1;
	double length = 50.0 / 2;
	double result[3];
	img.advanceAPoint(point, direction, flag, length, result);
	cout << "result: " << result[0] << " " << result[1] << " " << result[2] << endl;
*/
/*
int i;
cin >> i;
cout << i << endl;
*/
/*	visTransferFunction ddd(&md);
	trsferFunc.inputThresholds();
*/	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 800);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	cout << "Initializing..." << endl;
	init();
/*	
	cout << "The image: " << endl;
	for(int i = 0; i < imageSize; i++)
		for(int j = 0; j < imageSize; j++)
		{
			if(i % 30 == 0 && j % 30 == 0)
				cout <<	(int)_image[i][j][0] << "	" << (int)_image[i][j][1] << "	" << (int)_image[i][j][2] << endl; 
		}
*/
	glutDisplayFunc(display);
	cout << "Displaying..." << endl;
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutMainLoop();


/**************test a member function *****************/
/*
	double a[3];
	cout << "Input a direction please." << endl;
	for(int i = 0; i < 3; i++)
	{
		cin >> a[i];
	}

	double b[3];
	aaa.getOrthogonalDirection(a, b);
	cout << "The result is : " << endl
		 << b[0] << endl
	     << b[1] << endl
		 << b[2] << endl;

	double c[3];
	double d[3];
	cout << "input for c: " << endl;
	for(int i = 0; i < 3; i++)
		cin >> c[i];
	cout << "input for d: " << endl;
	for(int i = 0; i < 3; i++)
		cin >> d[i];

	double f[3];
	aaa.crossMultiply(c, d, f);
	cout << "the cross result: " << endl
	     << f[0] << endl
		 << f[1] << endl
		 << f[2] << endl;
*/
	return 0;
}

